<template>
  <div class="pages-container">

    <!-- 搜索框 Or 工具框 -->
    <a-form class="pages-tools-box" :form="form" @submit="getSearch">
      <a-row>
        <a-col :span="24" :style="{ textAlign: 'right' }">
          <a-button :style="{ marginLeft: '8px' }" @click="edit()">新增</a-button>
        </a-col>
      </a-row>
    </a-form>

    <!-- 表格 -->
    <div class="pages-content">
      <a-table :columns="columns" :data-source="data" :pagination="false" rowKey="id" :loading="tableLoading">
        <a-space slot="groupId" slot-scope="row">{{ row.group && row.group.title }}</a-space>
        <a-space slot="action" slot-scope="record">
          <a @click="edit(record.id)">编辑</a>
          <a v-if="record.id != 1" @click="del(record.id)">删除</a>
        </a-space>
      </a-table>
    </div>

    <!-- 分页 -->
    <a-pagination class="pagination" show-quick-jumper :default-current="current_page" :total="total"
                  @change="changePages"/>
  </div>
</template>

<script>
const columns = [
  {title: 'ID', dataIndex: 'id'},
  {title: '用户名', dataIndex: 'username'},
  {title: '姓名', dataIndex: 'name'},
  {title: '手机号', dataIndex: 'mobile'},
  {title: '所属角色', scopedSlots: {customRender: 'groupId'}},
  {title: '操作', scopedSlots: {customRender: 'action'}},
];

import config from "../../tools/config";

export default {
  name: "Message.vue",
  data() {
    return {
      // 公用配置
      Config: {},
      // 分页数据
      current_page: 1,
      total: 100,
      // table数据
      tableLoading: false,
      columns,
      data: [],
      // 搜索表单
      form: this.$form.createForm(this),
      // other
      UserStateList: [],
    };
  },
  created() {
    this.Config = config
    this.getData()
  },
  methods: {

    /**
     * 进行搜索
     * @param e
     */
    getSearch(e) {
      e.preventDefault();
      this.form.validateFields((error) => {
        if (!error) {
          this.getData(1)
        }
      });
    },

    /**
     * 请求Table数据
     * @param pages
     */
    getData(pages = 1) {
      this.tableLoading = true
      this.Http.get('/admin/admin/index', {
        page: pages,
        ...this.form.getFieldsValue()
      }).then(res => {
        this.tableLoading = false
        if (res.code === 1) {
          this.data = res.data.data
          this.current_page = res.data.current_page
          this.total = res.data.total
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => {
        this.tableLoading = false
      })
    },

    /**
     * 切换分页时
     * @param event
     */
    changePages(event) {
      this.getData(event)
    },

    /**
     * 清空搜索项
     */
    handleReset() {
      this.form.resetFields();
      this.getData()
    },

    edit(id = '') {
      this.$router.push("/Home/Admin/AccountDetail?id=" + id)
    },

    del(id) {
      const _this = this
      this.$confirm({
        content: '是否确认删除？',
        onOk() {
          _this.Http.get("/admin/admin/delete", {
            id: id
          }).then(res => {
            if (res.code == 1) {
              _this.getData(_this.current_page)
              _this.$message.success(res.msg)
            } else {
              _this.$message.error(res.msg)
            }
          })
        },
      });
    },

  },
}
</script>

<style scoped>

</style>
